package com.gitee.fmadmin.controller.order;

import com.gitee.fastmybatis.core.PageInfo;
import com.gitee.fastmybatis.core.query.LambdaQuery;
import com.gitee.fmadmin.common.req.IdParam;
import com.gitee.fmadmin.common.resp.Result;
import com.gitee.fmadmin.common.util.CopyUtil;
import com.gitee.fmadmin.controller.order.param.OrderInfoAddParam;
import com.gitee.fmadmin.controller.order.param.OrderInfoSearchParam;
import com.gitee.fmadmin.controller.order.param.OrderInfoUpdateParam;
import com.gitee.fmadmin.controller.order.vo.OrderInfoVO;
import com.gitee.fmadmin.dao.entity.OrderInfo;
import com.gitee.fmadmin.service.order.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author thc
 */
@RestController
@RequestMapping("order")
public class OrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 分页查询
     *
     * @param param 查询参数
     * @return 返回分页结果
     */
    @GetMapping("/page")
    public Result<PageInfo<OrderInfoVO>> page(OrderInfoSearchParam param) {
        LambdaQuery<OrderInfo> query = param.toLambdaQuery(OrderInfo.class);
        PageInfo<OrderInfoVO> pageInfo = orderInfoService.doPage(query)
          .convert(data -> CopyUtil.copyBean(data, OrderInfoVO::new));
        return Result.ok(pageInfo);
    }
    
    /**
     * 新增记录
     *
     * @param param 表单参数
     * @return 返回添加后的主键值
     */
    @PostMapping("/add")
    public Result<Long> add(@Validated @RequestBody OrderInfoAddParam param) {
        OrderInfo orderInfo = CopyUtil.copyBean(param, OrderInfo::new);
        orderInfoService.save(orderInfo);
        // 返回添加后的主键值
        return Result.ok(orderInfo.getId());
    }
    
    /**
     * 修改记录
     *
     * @param param 表单数据
     * @return 返回影响行数
     */
    @PostMapping("/update")
    public Result<Integer> update(@Validated @RequestBody OrderInfoUpdateParam param) {
        OrderInfo orderInfo = CopyUtil.copyBean(param, OrderInfo::new);
        return Result.ok(orderInfoService.update(orderInfo));
    }
					        
    /**
     * 删除记录
     *
     * @param param 参数
     * @return 返回影响行数
     */
    @PostMapping("/delete")
    public Result<Integer> delete(@Validated @RequestBody IdParam param) {
        return Result.ok(orderInfoService.deleteById(param.getId()));
    }
    
									
    
}